#include "http_client.h"

HttpClient::HttpClient(const std::string& host, int port) {
    cli_ = std::make_unique<httplib::SSLClient>(host, port);
}

void HttpClient::setDefaultHeaders(const httplib::Headers& headers) {
    default_headers_ = headers;
    cli_->set_default_headers(headers);
}

void HttpClient::setTimeout(int sec) {
    cli_->set_connection_timeout(sec, 0);
    cli_->set_read_timeout(sec, 0);
    cli_->set_write_timeout(sec, 0);
}

void HttpClient::setFollowRedirect(bool on) {
    cli_->set_follow_location(on);
}

HttpResponse HttpClient::get(const std::string& path, const httplib::Headers& headers) {
    HttpResponse resp;
    auto res = cli_->Get(path.c_str(), headers);
    if (res) {
        resp.status = res->status;
        resp.headers = res->headers;
        resp.body = res->body;
        resp.ok = true;
    } else {
        resp.error = httplib::to_string(res.error());
    }
    return resp;
}

HttpResponse HttpClient::post(const std::string& path, const std::string& body,
                             const std::string& content_type, const httplib::Headers& headers) {
    HttpResponse resp;
    auto res = cli_->Post(path.c_str(), headers, body, content_type.c_str());
    if (res) {
        resp.status = res->status;
        resp.headers = res->headers;
        resp.body = res->body;
        resp.ok = true;
    } else {
        resp.error = httplib::to_string(res.error());
    }
    return resp;
}

HttpResponse HttpClient::post(const std::string& path, const httplib::Params& params,
                             const httplib::Headers& headers) {
    HttpResponse resp;
    auto res = cli_->Post(path.c_str(), headers, params);
    if (res) {
        resp.status = res->status;
        resp.headers = res->headers;
        resp.body = res->body;
        resp.ok = true;
    } else {
        resp.error = httplib::to_string(res.error());
    }
    return resp;
}

HttpResponse HttpClient::put(const std::string& path, const std::string& body,
                            const std::string& content_type, const httplib::Headers& headers) {
    HttpResponse resp;
    auto res = cli_->Put(path.c_str(), headers, body, content_type.c_str());
    if (res) {
        resp.status = res->status;
        resp.headers = res->headers;
        resp.body = res->body;
        resp.ok = true;
    } else {
        resp.error = httplib::to_string(res.error());
    }
    return resp;
}

HttpResponse HttpClient::del(const std::string& path, const httplib::Headers& headers) {
    HttpResponse resp;
    auto res = cli_->Delete(path.c_str(), headers);
    if (res) {
        resp.status = res->status;
        resp.headers = res->headers;
        resp.body = res->body;
        resp.ok = true;
    } else {
        resp.error = httplib::to_string(res.error());
    }
    return resp;
}

void HttpClient::enable_server_certificate_verification(bool enabled) {
    cli_->enable_server_certificate_verification(enabled);
}

HttpResponse HttpClient::post(const std::string& path,
                             const httplib::MultipartFormDataItems& items,
                             const httplib::Headers& headers) {
    HttpResponse resp;
    auto res = cli_->Post(path.c_str(), headers, items);
    if (res) {
        resp.status = res->status;
        resp.headers = res->headers;
        resp.body = res->body;
        resp.ok = true;
    } else {
        resp.error = httplib::to_string(res.error());
    }
    return resp;
}